package com.zmn.oms.task.job.wechat;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.business.interfaces.wechat.WechatDataMigrationBService;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.dto.order.create.CreateOrderFailMessage;
import com.zmn.oms.model.entity.wechat.WechatMongo;
import com.zmn.oms.model.entity.wechat.WechatMongoQuery;
import com.zmn.oms.model.entity.wechatwork.OmsOrderWechatWorkLog;
import com.zmn.oms.services.interfaces.order.OrderChangeService;
import com.zmn.oms.services.interfaces.wechat.WechatMongoService;
import com.zmn.oms.services.interfaces.wechatwork.OmsOrderWechatWorkLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 类描述：企业微信数据迁移 mongoDB -> mysql
 *
 * @author weilai
 * @date 2022年10月10日 15:27:48
 */
@Slf4j
@LTS
public class WechatDataMigrationJob {

    @Resource
    private TaskManager taskManager;

    @Resource
    private WechatDataMigrationBService wechatDataMigrationBService;

    @PostConstruct
    public void init() throws InterruptedException {
        // 2022-10-14 02:00:00 执行一次
        addJob(TaskConsts.TASK_ID_WECHAT_DATA_MIGRATION_JOB, "0 0 2 14 10 ? 2022");
    }

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_WECHAT_DATA_MIGRATION_JOB)
    public Result  wechatDataMigration(Job job) throws InterruptedException {
        wechatDataMigrationBService.wechatDataMigration();
        log.info("迁移企业微信数据,结束");
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }

    /**
     * 增加定时任务，传入任务ID、执行时间表达式
     *
     * @param taskId         任务ID
     * @param cronExpression 执行时间表达式
     */
    protected void addJob(String taskId, String cronExpression) {
        log.info("添加任务:{}",taskId);
        Job job = new Job();
        job.setTaskId(taskId);
        job.setParam(TaskManager.SHARD_FIELD, taskId);
        job.setCronExpression(cronExpression);
        job.setReplaceOnExist(true);

        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#master_heart# job submit fail...");
        }
    }
}
